package com.stylefeng.guns.modular.charon.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.stylefeng.guns.common.persistence.dao.AccountIpMapMapper;
import com.stylefeng.guns.common.persistence.dao.PcServerInfoMapper;
import com.stylefeng.guns.common.persistence.model.AccountIpMap;
import com.stylefeng.guns.common.persistence.model.PcServerInfo;
import com.stylefeng.guns.common.persistence.model.UserLoginHis;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.CheckTip;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.base.tips.Tip;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.charon.service.IAccountIpMapService;
import com.stylefeng.guns.modular.charon.service.IPcServerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;


@Controller
@RequestMapping("/appSynchro")
public class AppSynchroController extends BaseController {

    @Resource
    PcServerInfoMapper pcServerInfoMapper;


    @RequestMapping(value = "/synchroPcIP", method = RequestMethod.POST)
    @ResponseBody
    public Object synchroPcIP(String account) {

        Tip tip = null;

        Map<String, String> map = this.pcServerInfoMapper.queryServerIpByaccount(account);
        if (ToolUtil.isEmpty(map)) {
            tip = new ErrorTip(400, "云端相匹配的服务器，请联系管理员!");
        } else {
            tip = new CheckTip(map.get("ip").toString());
        }

        UserLoginHis userLoginHis = new UserLoginHis();
        userLoginHis.setId(IdWorker.get32UUID());
        userLoginHis.setAccount(account);
        userLoginHis.setLoginTime(new Date());
        userLoginHis.setResult(tip.getCode() == 200 ? "1" : "0");

        return tip;
    }

}
